package com.dajie.pt.controller.edm;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dajie.common.DictionaryServicesContext;
import com.dajie.common.dictionary.DictEnum;
import com.dajie.common.dictionary.model.Dict;
import com.dajie.common.dictionary.service.DictService;
import com.dajie.common.framework.spring.controller.BaseController;
import com.dajie.corp.api.CorpServicesContext;
import com.dajie.corp.api.service.CorpService;
import com.dajie.corpreview.api.model.CorpreviewReviewDetail;
import com.dajie.corpreview.api.service.CorpReviewApiService;
import com.dajie.corpreview.client.CorpReviewServiceFactory;
import com.dajie.infra.user.model.UserBase;
import com.dajie.pt.model.EdmCorpReviewInfo;
import com.dajie.pt.service.EdmCorpReviewService;

@Controller
@RequestMapping("/edmsalary")
public class EdmSalaryController extends BaseController {

	protected static final Logger LOGGER = LoggerFactory.getLogger(EdmSalaryController.class);
	
	CorpService corpService = CorpServicesContext.getInstance().getCorpService();
	CorpReviewApiService corpReviewApiService = CorpReviewServiceFactory.getInstance().getCorpReviewApiService();
	
	static DictService dictService = DictionaryServicesContext.getInstance().getDictService();

	static Dict dictCity = dictService.getDict(DictEnum.DICT_CITY);
	
	static Dict dictSalary = dictService.getDict(DictEnum.DICT_SALARY);
	private final static int flag = 0;
	
	@Autowired
	EdmCorpReviewService<EdmCorpReviewInfo, Integer> edmCorpReviewService;
	
	
	@RequestMapping("index")
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "corpid", required = true) Integer corpId,
			@RequestParam(value = "uid", required = true) Integer uid,
			@RequestParam(value = "status", required = true) Integer status,
			@RequestParam(value = "ismember",required=false) Integer isMember) {
		
		LOGGER.info("====================" + this.getClass().getSimpleName());

		UserBase userBase = this.getLoginUser();
		if(userBase != null)
		{
			uid = userBase.getUid();
		}
		
		ModelAndView mv = new ModelAndView("edm/salary");

		
		long st = System.currentTimeMillis();
		long scoreSum=0;
		long impressionCareerSum=0;
		long impressionSkillSum=0;
		long impressionAtmosSum=0;
		long impressionPressureSum=0;
		long impressionProspectSum=0;
		EdmCorpReviewInfo edmCorpReviewInfo=null;
		Map<String, Object> map=new HashMap<String, Object>();
		String corpName = corpService.getCorpBaseById(corpId).getName();
		EdmCorpReviewInfo edmCorpReviewInfo1=edmCorpReviewService.getEdmCorpReviewByCorpId(corpId,flag);
		if(edmCorpReviewInfo1!=null){
		if(status==0){
			edmCorpReviewService.updateEdmCorpReviewYesByCorpId(corpId,flag);
		}else{
			edmCorpReviewService.updateEdmCorpReviewNoByCorpId(corpId,flag);
		}
		edmCorpReviewInfo=edmCorpReviewService.getEdmCorpReviewByCorpId(corpId,flag);
		}else{
			edmCorpReviewService.insertEdmCorpReviewNumByCorpId(corpId,flag);
			if(status==0){
				edmCorpReviewService.updateEdmCorpReviewYesByCorpId(corpId,flag);
			}else{
				edmCorpReviewService.updateEdmCorpReviewNoByCorpId(corpId,flag);
			}
			edmCorpReviewInfo=edmCorpReviewService.getEdmCorpReviewByCorpId(corpId,flag);
		}
		
		//得到公司初始评价
		map = corpReviewApiService.getCorpReviewMap(corpId);
		if (map != null && map.size() > 0) {
			if ((Integer) map.get("code") == 1) {
				scoreSum=Math.round((Double) map.get("scoreSum")*5);
				impressionCareerSum=Math.round((Double) map.get("impressionCareerSum")*5);
				impressionSkillSum=Math.round((Double) map.get("impressionSkillSum")*5);
				impressionAtmosSum=Math.round((Double) map.get("impressionAtmosSum")*5);
				impressionPressureSum=Math.round((Double) map.get("impressionPressureSum")*5);
				impressionProspectSum=Math.round((Double) map.get("impressionProspectSum")*5);
				
			}else{
				scoreSum=this.getRandomNum();
				impressionCareerSum=this.getRandomNum();
				impressionSkillSum=this.getRandomNum();
				impressionAtmosSum=this.getRandomNum();
				impressionPressureSum=this.getRandomNum();
				impressionProspectSum=this.getRandomNum();
			}
		}
		


		mv.addObject("corpName", corpName);
		//mv.addObject("reviewDetail", reviewDetail);
		mv.addObject("corpId", corpId);
		mv.addObject("scoreSum", scoreSum);
		mv.addObject("impressionCareerSum", impressionCareerSum);
		mv.addObject("impressionSkillSum", impressionSkillSum);
		mv.addObject("impressionAtmosSum", impressionAtmosSum);
		mv.addObject("impressionPressureSum", impressionPressureSum);
		mv.addObject("impressionProspectSum", impressionProspectSum);
		mv.addObject("edmCorpReviewInfo", edmCorpReviewInfo);
		mv.addObject("uid", uid);
		mv.addObject("status", status);
		mv.addObject("ismember", isMember);
		LOGGER.info("EdmController.index.timecost:{}",(System.currentTimeMillis()-st));
		return mv;
	}

	private int getRandomNum(){
		Random rd=new Random();//随机数生成器		
		return (int)(rd.nextDouble()*2)+4;
	}
	
	
	@RequestMapping("salaryReview")
	public ModelAndView salaryReview(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "statuses", required = true) Integer status,
			@RequestParam(value = "reason", required = true) String reason,
			@RequestParam(value = "uid", required = true) Integer uid,
			@RequestParam(value = "corpId", required = true) Integer corpId,
			@RequestParam(value = "impression", required = true) int impressionCorpScore,
			@RequestParam(value = "agree", required = true) int impressionCeoScore,
			@RequestParam(value = "career", required = true) int career,
			@RequestParam(value = "skill", required = true) int skill,
			@RequestParam(value = "workAtmos", required = true) int workAtmos,
			@RequestParam(value = "workPressure", required = true) int workPressure,
			@RequestParam(value = "prospect", required = true) int prospect,
			@RequestParam(value = "ismember", required = true) int isMember
			) {
		LOGGER.info("---------------------------------------------------");
		UserBase userBase = this.getLoginUser();
		if(userBase != null)
		{
			uid = userBase.getUid();
		}
		
		int isAnonymous=0;
		String topic="#公司是否亏待你了#";
		String reasonTopic=topic+reason;
		StringBuffer sbf=new StringBuffer(reasonTopic);
		for(int i=0;i<=(20-reasonTopic.length());i++){
		sbf.append(" ");
		}
		/*StringBuffer sbf=new StringBuffer(reason);
		for(int i=0;i<=(20-reason.length());i++){
			sbf.append(" ");
		}*/
		String reviewContent=sbf.toString();
		EdmCorpReviewInfo edmCorpReviewInfo=edmCorpReviewService.getEdmCorpReviewByCorpId(corpId,flag);
		CorpreviewReviewDetail corpReviewDetail=null;
		Map<String,Object> corpReviews=corpReviewApiService.addCorpReview(uid, corpId, isMember, impressionCorpScore, impressionCeoScore, isAnonymous, null, reviewContent);
		int[] impressions={career,skill,workAtmos,workPressure,prospect};
		Integer reviewId=null;
		if((Integer)corpReviews.get("code")==0){
			reviewId=(Integer) corpReviews.get("reviewId");
		}
		
		corpReviewApiService.impressionSave(uid, corpId, reviewId, impressions, reviewContent);
		List<Integer> idList=new ArrayList<Integer>();
		idList.add(reviewId);
		List<CorpreviewReviewDetail> reviewList=corpReviewApiService.getReviewListByIdList(idList);
		if(reviewList!=null&&!reviewList.isEmpty()){
			corpReviewDetail=reviewList.get(0);		
		}
		ModelAndView mv = new ModelAndView("edm/salaryReview");
		mv.addObject("status", status);
		mv.addObject("edmCorpReviewInfo", edmCorpReviewInfo);
		mv.addObject("reviewDetail", corpReviewDetail);
		mv.addObject("corpId", corpId);
		return mv;
	}
	}		

